package com.ebelter.bodyfatscale.util;


import android.app.Activity;
import android.text.TextUtils;

import java.util.ArrayList;
import java.util.List;

public class ActivityManagers {
    private static ActivityManagers instance;
    private List<Activity> activities;

    private ActivityManagers(){
        activities = new ArrayList<>();
    }


    public static ActivityManagers getInstance(){
        if (instance==null){
            synchronized (ActivityManagers.class){
                if (instance==null) instance=new ActivityManagers();
            }
        }
        return instance;
    }



    public void addActivity(Activity activity) {
        activities.add(activity);
    }
    public List<Activity> getActList() {
        return activities;
    }


    public void removeActivity(Activity activity) {
        if (activity==null){
            return;
        }
        for (int i = 0; i < activities.size(); i++) {
            if (activity.getLocalClassName().equals(activities.get(i).getLocalClassName())) {
                activities.remove(i);
            }
        }
    }


    public void closeActivity(String activityNameTag){
        if (TextUtils.isEmpty(activityNameTag)) {
            return;
        }

        if (activities!=null){
            for (int i=0;i<activities.size();i++){
                Activity activitie=activities.get(i);
                if (activitie!=null&&!activitie.isFinishing()&&activityNameTag.contains(activitie.getClass().getSimpleName())){
                    activitie.finish();
                    activities.remove(activitie);

                }
            }
        }
    }


    public void exit(){
        if (activities!=null){
            for (int i=0;i<activities.size();i++){
                Activity activitie=activities.get(i);
                if (activitie!=null&&!activitie.isFinishing()) activitie.finish();
            }
        }
    }







}
